package com.wanmait.schedule.controller;

import com.wanmait.schedule.entity.Test;
import com.wanmait.schedule.service.TestService;
import com.wanmait.schedule.util.AjaxResult;
import com.wanmait.schedule.util.ExcelUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/test")
public class TestController {

    @Resource
    private TestService testService;

    @PostMapping("/import")
    public AjaxResult importExcel(@RequestParam("file") MultipartFile file){
        List<Test> testList = ExcelUtils.excelToTest(file);
        // 遍历list,导入数据库。
        for (Test test : testList) {
            testService.save(test);
        }
        return AjaxResult.success("导入成功");
    }
}
